#import "ThreeOfAKindGame.h"

@interface ThreeOfAKindGame ()

@property NSArray* colors;
@property BOOL isGameOver;
@property NSMutableArray* images;
@property NSInteger currentImageToChange;

@end

@implementation ThreeOfAKindGame

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"ThreeOfAKindGame"];
        [self setType:reaction];
        self.ruleOfTheGame=@"Three shapes of the same kind!";
        
        self.images=[[NSMutableArray alloc]init];
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        
    }
    return self;
}


-(void)draw{
    [self removeAllComponents];
    
    
    for (int coloum=0; coloum<kNumberOfImageForRow; coloum++) {
        for (int row=0; row<kNumberOfColoums; row++) {

            int imageNumber=arc4random_uniform(kThreeOfAKindNumberOfImage);
            NSString* imageString=[NSString stringWithFormat:@"ThreeShapesOfSameKind%d",imageNumber+1];
            
            self.images[self.currentImageToChange]=imageString;
            int transform=arc4random_uniform(4);
            CGFloat trans=kTransKoef * transform;
            
            NSInteger imageSize=(kScreenWidth-kHeighMargin)/kNumberOfImageForRow;
            
            ImageGameComponent* image=[[ImageGameComponent alloc]initWithName:imageString andFrame:CGRectMake(kWidthMargin+(coloum*imageSize), kHeighMargin+(row*imageSize), imageSize, imageSize)andTrans:trans];
            [self addComponents:image];
            self.currentImageToChange++;
            
        }
    }
    
    self.currentImageToChange=0;
    if ([self isRight]) {
        [self draw];
    }
}

-(BOOL)isRight{
    return [self IsThereThreeOfSameKindInArray:self.images];
}

-(BOOL)IsThereThreeOfSameKindInArray:(NSMutableArray*)array{
    NSInteger br=0;
    for (int index=0;index<[array count] ; index++) {
        for (int other=index+1; other < [array count]; other++) {
            if ([[array objectAtIndex:index]isEqualToString:[array objectAtIndex:other]]) {
                br++;
            }
        }
        
        if (br > 1) {
            return YES;
        }
        else{
            br=0;
        }
    }
    return NO;
}

-(void)play{
    [self draw];
    
    NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval: 1                                                 target: self selector:@selector(newFace:)userInfo: nil repeats:YES];
    
}

-(void)newFace:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self changeOneImage];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames" object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}


-(void)changeOneImage{
    int imageNumber=arc4random_uniform(kThreeOfAKindNumberOfImage);
    NSString* imageString=[NSString stringWithFormat:@"ThreeShapesOfSameKind%d",imageNumber+1];
    
    self.images[self.currentImageToChange]=imageString;
    int transform=arc4random_uniform(4);
    CGFloat trans=kTransKoef * transform;
    ImageGameComponent* newComp=self.components[self.currentImageToChange];
    newComp.name=imageString;
    self.currentImageToChange++;
    
    if (self.currentImageToChange == kThreeOfAKindNumberOfImage) {
        self.currentImageToChange=0;
    }

    
}
@end
